package com.wln.kms.kms.service.impl;

import com.wln.common.utils.R;
import com.wln.kms.kms.dao.BargainActivityDao;
import com.wln.kms.kms.dao.BargainRecordDao;
import com.wln.kms.kms.entity.BargainActivityEntity;
import com.wln.kms.kms.entity.BargainRecordEntity;
import com.wln.kms.kms.util.IdWorker;
import com.wln.kms.kms.vo.BargainVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wln.common.utils.PageUtils;
import com.wln.common.utils.Query;

import com.wln.kms.kms.dao.BargainOrderDao;
import com.wln.kms.kms.entity.BargainOrderEntity;
import com.wln.kms.kms.service.BargainOrderService;

import static jdk.nashorn.internal.runtime.GlobalFunctions.parseInt;


@Service("bargainOrderService")
public class BargainOrderServiceImpl extends ServiceImpl<BargainOrderDao, BargainOrderEntity> implements BargainOrderService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private RedisTemplate redisTemplate;

    @SuppressWarnings("all")
    @Autowired
    private BargainRecordDao bargainRecordDao;

    @SuppressWarnings("all")
    @Autowired
    private BargainActivityDao bargainActivityDao;

    @SuppressWarnings("all")
    @Autowired
    private BargainOrderService bargainOrderService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<BargainOrderEntity> wrapper = new QueryWrapper<>();
        String userid = (String) params.get("userid");
        wrapper.eq("user_id", userid);
        IPage<BargainOrderEntity> page = this.page(
                new Query<BargainOrderEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public R add(BargainOrderEntity bargainOrder) {
        QueryWrapper<BargainOrderEntity> w = new QueryWrapper<>();
        w.eq("user_id", bargainOrder.getUserId()).eq("activity_hid", bargainOrder.getActivityHid());
        List<BargainOrderEntity> list = bargainOrderService.list(w);


        if(list.size()==0){

            bargainOrder.setHorderId(idWorker.nextId());
            Number a=0.2;
            Number b=0.1;
            BigDecimal max = new BigDecimal(a.toString());
            BigDecimal min = new BigDecimal(b.toString());
            BigDecimal max1 = bargainOrder.getGoodsPrice().multiply(max);
            BigDecimal min1 = bargainOrder.getGoodsPrice().multiply(min);
            double v = parseInt(Math.random() * ((max1.subtract(min1)).intValue() + 1)+min1.intValue(), 10);
            bargainOrder.setHorderYprice(new BigDecimal(v));
            BigDecimal subtract = bargainOrder.getGoodsPrice().subtract(bargainOrder.getHorderYprice());
            bargainOrder.setHorderPrice(subtract);
            bargainOrder.setHorderAdd("12");
            bargainOrder.setHorderStatus(0);
            bargainOrder.setCreateDate(new Date());
            baseMapper.insert(bargainOrder);

            BargainRecordEntity entity = new BargainRecordEntity();
            entity.setActivityHid(bargainOrder.getActivityHid());
            entity.setGoodsId(bargainOrder.getGoodsId());
            entity.setUserId(bargainOrder.getUserId());
            entity.setBargainBprice(bargainOrder.getHorderYprice());
            entity.setCount(1);
            entity.setGoodsPrice(bargainOrder.getGoodsPrice());
            entity.setBargainPrice(bargainOrder.getHorderPrice());
            entity.setGoodsImg(bargainOrder.getGoodsImg());
            entity.setRecordStatus(1);
            bargainRecordDao.insert(entity);
            BoundHashOperations goods = redisTemplate.boundHashOps("goods");

            Integer o = (Integer) goods.get(bargainOrder.getGoodsId());

            if(o>0){

                goods.put(bargainOrder.getGoodsId(),o-bargainOrder.getGoodsCount());
            }

            return R.ok("添加成功");
        }
        return R.error("该订单已经添加过");
    }

    @Override
    public PageUtils find(Map<String, Object> params) {
        QueryWrapper<BargainOrderEntity> wrapper = new QueryWrapper<>();
        String userid = (String) params.get("userid");
        String bargainId = (String) params.get("bargainId");
        wrapper.eq("user_id", userid);
        wrapper.eq("horder_id",bargainId);
        IPage<BargainOrderEntity> page = this.page(
                new Query<BargainOrderEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public R cut(BargainOrderEntity bargainOrder,String userid) {
        BargainRecordEntity recordEntity = bargainRecordDao.selectOne(new QueryWrapper<BargainRecordEntity>().eq("user_id", userid).eq("activity_hid", bargainOrder.getActivityHid()));
        BargainRecordEntity selectOne = bargainRecordDao.selectOne(new QueryWrapper<BargainRecordEntity>().eq("order_id", recordEntity.getOrderId()).eq("user_id", bargainOrder.getUserId()));
        if(selectOne==null){
            Number a=0.2;
            Number b=0.1;
            BigDecimal max = new BigDecimal(a.toString());
            BigDecimal min = new BigDecimal(b.toString());
            BigDecimal max1 = bargainOrder.getGoodsPrice().multiply(max);
            BigDecimal min1 = bargainOrder.getGoodsPrice().multiply(min);
            double v = parseInt(Math.random() * ((max1.subtract(min1)).intValue() + 1)+min1.intValue(), 10);

            BargainActivityEntity activityEntity = bargainActivityDao.selectById(bargainOrder.getActivityHid());

            BargainRecordEntity entity = new BargainRecordEntity();
            entity.setBargainBprice(new BigDecimal(v));
            BigDecimal subtract = bargainOrder.getHorderPrice().subtract(entity.getBargainBprice());
            entity.setBargainPrice(subtract);
            entity.setCount(1);
            entity.setRecordStatus(1);
            entity.setGoodsImg(recordEntity.getGoodsImg());
            entity.setUserId(bargainOrder.getUserId());
            entity.setGoodsPrice(recordEntity.getGoodsPrice());
            entity.setGoodsId(recordEntity.getGoodsId());
            entity.setActivityHid(recordEntity.getActivityHid());
            entity.setOrderId(recordEntity.getOrderId());
            bargainRecordDao.insert(entity);

            BargainOrderEntity orderEntity = baseMapper.selectOne(new QueryWrapper<BargainOrderEntity>().eq("user_id", userid).eq("activity_hid", bargainOrder.getActivityHid()));
            orderEntity.setHorderYprice(orderEntity.getHorderYprice().add(new BigDecimal(v)));
            orderEntity.setHorderPrice(subtract);
            baseMapper.updateById(orderEntity);
            return R.ok();
        }
        return R.error("不能重复砍价");

    }


}